package com.maybach.core.dubbo.consumer.adapter;

import com.audi.dubbo.api.common.Result;
import com.audi.dubbo.api.item.intf.B2BCarService;
import com.audi.dubbo.api.item.request.B2BCarRequest;
import com.audi.dubbo.api.item.response.impl.B2BCar;
import com.audi.dubbo.api.item.response.impl.BaseValue;
import com.audi.dubbo.api.item.response.impl.InduceErrorReq;
import com.maybach.core.dubbo.consumer.annotation.Monitor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by wenhua on 16/2/26.
 */
@Component
@Monitor(serviceName = "b2bCarService", desc = "发布车源dubbo服务")
public class B2BCarServiceDubboAdapter {
	@Autowired
	private B2BCarService b2BCarService;
	private final Log log = LogFactory.getLog(this.getClass());

	/**
	 * 从接口导入
	 * 
	 * @param requests
	 * @return
	 */
	public Result<List<InduceErrorReq>> induceCar(List<B2BCarRequest> requests) {
		Result<List<InduceErrorReq>> listResult;
		try {
			listResult = b2BCarService.induceWithSynonym(requests);
		} catch (Exception e) {
			log.error("调用induceCar导入车源接口异常", e);
			return null;
		}
		return listResult;
	}

	public Long induceAsy(List<B2BCarRequest> requests) {
		try {
			Result<Long> induceRes = b2BCarService.induceAsyn(requests);
			if (induceRes != null && induceRes.isSuccess()) {
				return induceRes.getData();
			} else {
				return -1L;
			}
		} catch (Exception e) {
			log.error("调用induceAsy导入车源接口异常", e);
			return -1L;
		}
	}

	/**
	 * 发布新车源
	 * 
	 * @param requests
	 * @return
	 */
	public Result<Long> publishCar(List<B2BCarRequest> requests) {
		Result<Long> result;
		try {
			result = b2BCarService.publishCar(requests);
		} catch (Exception e) {
			log.error("调用publishCar发布车源接口异常", e);
			return null;
		}
		return result;
	}

	/**
	 * 更新车源信息
	 * 
	 * @param request
	 * @param carId
	 * @return
	 */
	public Result<Boolean> editCar(B2BCarRequest request, long carId) {
		Result<Boolean> result;
		try {
			result = b2BCarService.editCar(request, carId);
		} catch (Exception e) {
			log.error("调用editCar编辑车源接口异常", e);
			return null;
		}
		return result;
	}

	/**
	 * 获取单辆车源信息
	 * 
	 * @param carId
	 * @return
	 */
	public Result<B2BCar> querySingleCar(long carId) {
		Result<B2BCar> result;
		try {
			result = b2BCarService.querySingleCar(carId);
		} catch (Exception e) {
			log.error("调用querySingleCar查询车源接口异常", e);
			return null;
		}
		return result;
	}

	/**
	 * 根据车系id获取对应的外观内饰
	 * 
	 * @param seriesId
	 * @return
	 */
	public Result<Map<String, List<BaseValue>>> getTrimAndFacade(long seriesId) {
		Result<Map<String, List<BaseValue>>> result;
		try {
			result = b2BCarService.getTrimAndFacade(seriesId);
		} catch (Exception e) {
			log.error("调用getTrimAndFacade查询车源接口异常", e);
			return null;
		}
		return result;
	}

	/**
	 * 根据车型获取官方指导价
	 * 
	 * @param modelId
	 * @return
	 */
	public Result<String> getGuidePrice(long modelId) {
		Result<String> result;
		try {
			result = b2BCarService.getGuidePrice(modelId);
		} catch (Exception e) {
			log.error("调用getGuidePrice查询车源接口异常", e);
			return null;
		}
		return result;
	}
}
